package com.foodchain.service;

import com.foodchain.entity.ActivitySchedule;
import com.foodchain.repository.ActivityScheduleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;

/**
 * @ClassName ActivityScheduleService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/22 14:09
 * @Version 1.0
 **/
@Service
public class ActivityScheduleService {
    @Resource
    private ActivityScheduleRepository repository;

    public Page<ActivitySchedule> findList(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createDate");
        Specification<ActivitySchedule> spec = new Specification<ActivitySchedule>() {        //查询条件构造
            public Predicate toPredicate(Root<ActivitySchedule> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Boolean> status= root.get("status");
                Predicate p1 = cb.isTrue(status);
                Predicate p = cb.and(p1);
                return p;
            }
        };
        return repository.findAll(spec,pageable);
    }
}
